<?php
include(Yii::getPathOfAlias('application.vendors.ajax-uploader.server').'/include.php');
/**
 * This is the model class for table "documents".
 *
 * The followings are the available columns in table 'documents':
 * @property integer $id
 * @property string $name
 * @property string $file_loc
 * @property integer $foreign_key
 * @property string $for_model
 * @property string $remark
 */
class documents extends ActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return documents the static model class
     */
    public $uploaderFolder = null;

    public $sizeLimit = 5242880;
    public $allowedExtensions=array('jpg','jpeg','png','tif','pdf','gif','txt','doc');

    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'documents';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, file_loc, foreign_key, for_model, date', 'required'),
            array('foreign_key, date', 'numerical', 'integerOnly' => true),
            array('name, for_model, type, ext_type', 'length', 'max' => 50),
            array('file_loc', 'length', 'max' => 2048),
            array('remark', 'length', 'max' => 125),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, file_loc, foreign_key, for_model, remark', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    public function upload(){
        if($this->uploaderFolder == null)
            $this->uploaderFolder = Yii::app()->getBasePath().'/uploads/'.Yii::app()->session->get('domain').'/';
        if(!is_dir($this->uploaderFolder))
            mkdir($this->uploaderFolder);
        $uploader = new qqFileUploader($this->allowedExtensions, $this->sizeLimit);
        $result = $uploader->handleUpload($this->uploaderFolder);
        if(isset($result['success'])){
            $this->name = $uploader->getName();
            $this->file_loc = $this->uploaderFolder.$uploader->getFileName();
            $this->date = time();
            $this->type = 'Others';
            $this->ext_type = mime_content_type ($this->file_loc);
            if(!$this->save()){
                unlink($this->uploaderFolder.'/'.$this->file_loc);
                return false;
            }
            else
                return true;
        }
        else
            return false;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Name',
            'file_loc' => 'File Loc',
            'foreign_key' => 'Foreign Key',
            'for_model' => 'For Model',
            'remark' => 'Remark',
        );
    }

    public function delete(){
        $filename = $this->file_loc;
        if(parent::delete()){
            unlink($filename);
            return true;
        }
        else
            return false;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);

        $criteria->compare('name', $this->name, true);

        $criteria->compare('file_loc', $this->file_loc, true);

        $criteria->compare('foreign_key', $this->foreign_key);

        $criteria->compare('for_model', $this->for_model, true);

        $criteria->compare('remark', $this->remark, true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

}